package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsDayData;
import com.ningxia.water.entity.TsWellWarn;
import com.ningxia.water.enums.WarnCodeEnum;
import com.ningxia.water.enums.WarnStatusEnum;
import com.ningxia.water.mapper.*;
import com.ningxia.water.service.ITsDayDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.vo.in.ApplicantWarnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.ListIterator;

/**
 * 服务实现类
 *
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsDayDataServiceImpl extends ServiceImpl<TsDayDataMapper, TsDayData> implements ITsDayDataService {

    @Autowired
    private TsDayDataMapper dayDataMapper;

    @Autowired
    private TsMonthDataMapper monthDataMapper;

    @Autowired
    private TsWellWarnMapper wellWarnMapper;

    @Autowired
    private TsApplicantWellMapper applicantWellMapper;

    @Autowired
    private TsWellInfoMapper wellInfoMapper;

    @Override
    public Double findVolumeByWellIdAndTime(Integer well_id, String startTime, String endTime) {
        return dayDataMapper.findVolumeByWellIdAndTime(well_id, startTime, endTime);
    }

    @Override
    public Double findVolumeByYear(Integer well_id, String year) {
        String startTime = year + "-01-01";
        String endTime = year + "-12-31";
        return dayDataMapper.findVolumeByWellIdAndTime(well_id, startTime, endTime);
    }


    @Override
    public Double findVolumeByTime(Integer well_id, String type, String time) {
        Double res = null;
        if ("year".equals(type)) {
            res = this.findVolumeByYear(well_id, time);
        } else if ("month".equals(type)) {
            res = monthDataMapper.getMonthVolumeByWellId(well_id, time);
        } else {
            res = dayDataMapper.findVolumeByWellIdAndDay(well_id, time);
        }
        return res;
    }

    /**
     * 水量超额报警
     *
     * @return
     */
    @Override
    public void getVolumeExcessWarnInfos() throws Exception {
        //今年
        int year = LocalDateTime.now().getYear();
        List<ApplicantWarnVo> list = dayDataMapper.getApplicantPlanVolumeByYear(String.valueOf(year));
        ListIterator<ApplicantWarnVo> it = list.listIterator();
        while (it.hasNext()) {
            ApplicantWarnVo vo = it.next();
            //今年第一天
            String startTime = LocalDateTime.now().withDayOfYear(year)
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            //前一天
            String endTime = LocalDateTime.now().minusDays(1)
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            Double totalVolume = dayDataMapper.findTotalVolumesByApplicantId(vo.getApplicantId(), startTime, endTime);
            if (totalVolume >= (vo.getPlanVolume() * 0.9)) {
                try {
                    wellWarnMapper.insert(new TsWellWarn(WarnCodeEnum.VOLUME_EXPRESS.getCode(),
                            WarnStatusEnum.UNTREATED.getCode(), LocalDateTime.now(), vo.getApplicantId()));
                    //将用户对应的机井全部设为4444状态
                    List<Integer> wellList = applicantWellMapper.getWellIdsByApplicant(vo.getApplicantId());
                    ListIterator<Integer> its = wellList.listIterator();
                    while (its.hasNext()) {
                        //设置机井状态为4444
                        wellInfoMapper.setWellStatusByWellId(its.next(), 4444);
                    }
                } catch (Exception e) {
                    throw new Exception("执行错误");
                }

            }
        }
    }
}
